home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1988 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.4 KB

  1. From: SimsGW@msn.com (Gary Sims)
  2. Subject: RE: Function body banned in .H file - Can I still inline?
  3. Date: 14 Jan 96 21:42:20 -0800
  4. References: <4d0ir0$68e@newsroom.hitc.com>
  5. Message-ID: <00001a81+00008dc1@msn.com>
  6. Path: news.msn.com!msn.com
  7. Newsgroups: comp.lang.c++
  8. Organization: The Microsoft Network (msn.com)
  9.  
  10. I think you've misunderstood something you read, Chris. You can put 
  11. the body of a function in the header file. That's the only practical 
  12. way to have inline functions. What you can NOT do is present the body 
  13. to the compiler more than once. Since the same header is frequently 
  14. #include'd by one more than one module, you must prevent it being 
  15. interpreted twice. Here's how you do that. I've included an example 
  16. of the two ways to specify an inline function:
  17.  
  18. #ifndef THIS_HEADER_NAME
  19. #define THIS_HEADER_NAME
  20.  
  21. class Foo
  22. {
  23.    int PrivateXValue;
  24. public:
  25.    Foo();
  26.    int GetX(){return PrivateXValue;};
  27.    int GetFractionalX(const int Divisor);
  28. };
  29.  
  30. int inline Foo::GetFractionalX(const int Divisor)
  31. {
  32.    return PrivateXValue/Divisor;
  33. };
  34.  
  35. #endif
  36.  
  37. The first time the compiler sees this, the value THIS_HEADER_NAME 
  38. will not have been defined, so the lines between there and the #endif 
  39. will be processed. On successive occasions, it WILL be defined, so 
  40. they won't. That avoids the problem of double definitions of the 
  41. function body. Hope this helps,
  42.  
  43. Gary W. Sims
  44. Stonehaven Laboratory
  45.